//
//  ViewController.m
//  uploadDemo
//
//  Created by student on 16/3/30.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"

#define kBoundary @"hejiaye"

#define KNewLine @"\r\n"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self uploadFile];
}

- (void)uploadFile
{
    NSURL *url = [NSURL URLWithString:@"http://192.168.13.14:8080/MJServer/upload"];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    request.HTTPMethod = @"POST";
    
    NSString *headerStr = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",kBoundary];
    
    [request setValue:headerStr forHTTPHeaderField:@"Content-Type"];
    
    NSMutableData *mData = [NSMutableData data];
    
    NSMutableString *mStr = [NSMutableString string];
    
    [mStr appendString:KNewLine];
    [mStr appendString:@"--"];
    [mStr appendString:kBoundary];
    [mStr appendString:KNewLine];
    [mStr appendString:@"Content-Disposition: form-data; name=\"file\"; filename=\"2.txt\""];
    [mStr appendString:KNewLine];
    [mStr appendString:@"Content-Type: text/plain"];
    [mStr appendString:KNewLine];
    [mStr appendString:KNewLine];
    NSLog(@"%@",mStr);
    
    [mData appendData:[mStr dataUsingEncoding:NSUTF8StringEncoding]];
    NSString *filepath = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:filepath];
    [mData appendData:data];
    
    mStr = [NSMutableString string];
    [mStr appendString:KNewLine];
    [mStr appendString:@"--"];
    [mStr appendString:kBoundary];
    [mStr appendString:KNewLine];
    [mStr appendString:@"Content-Disposition: form-data; name=\"username\""];
    [mStr appendString:KNewLine];
    [mStr appendString:KNewLine];
    [mStr appendString:@"abc"];
    [mStr appendString:KNewLine];
    //4. 结尾
    [mStr appendString:@"--"];
    [mStr appendString:kBoundary];
    [mStr appendString:@"--"];
    [mStr appendString:KNewLine];
    NSLog(@"%@",mStr);
    [mData appendData:[mStr dataUsingEncoding:NSUTF8StringEncoding]];
    
    request.HTTPBody = mData;
    
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError)
    {
        if (connectionError == nil)
        {
            NSError *error;
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
            if (error != nil)
            {
                NSLog(@"%@",dict);
            }
            else
            {
                NSLog(@"%@",[error localizedDescription]);
            }
        }
        else
        {
            NSLog(@"%@",[connectionError localizedDescription]);
        }
    }];
    
}


@end
